var gameData = require('../scripts/network/gameData');
var playerData = require('../scripts/network/playerData');
var response = require('../scripts/network/MatchvsResponse');
var msg = require('../scripts/network/MatchvsMessage');
var engine = require('../scripts/network/MatchvsEngine');
var checkCard = require('../scripts/checkCard');
var jiesuan = require('../scripts/jiesuan');
//const A = require('../scripts/audio');
const B = require('../scripts/xipaifapai');  //通过这个方法对发牌脚本进行调用
var caozuoChildrenButton = new Array;
var caozuoMsg = {             //记录点击按钮的操作
    isReady: false,
    isJiaodizhu: false,
    isQiangdizhu: false,
    isChupai: false,
}
var ready = false;             //在update中用于控制 只执行一次
var cardArray = new Array;
var dizhupaiEar;
var cardLeft;                   //顶部左边的卡片     
var cardCenter;                //中间的卡片
var cardRight;                  //右边的卡片
var SITDOWN = 2;
var STANDUP = 1;

//var addDizhupai= new Array;
var oldArr = new Array;         //用于存储初始的玩家的牌的数据
var newArr = new Array;         //用于存储添加地主牌后的玩家牌数据
var clickCard = new Array;      //用于存储本玩家点击的牌
var recordClickCard = new Array;
var otherCard = new Array;      //用于存储上一个玩家出的牌的数据
var displayCard = new Array;    //用于存储玩家出的牌的 prefab

var audio;

var timeIndex = 0;
var remainTime = 25;

cc.Class({
    extends: cc.Component,

    properties: {
        player1: cc.Node,           //player1 节点
        player2: cc.Node,           //player2 节点
        player3: cc.Node,           //player3 节点
        roomId: cc.Label,           //房间ID Label
        roomName: cc.Label,         //房间名 Label
        player1Avatar: cc.Sprite,   //player1 头像
        player1Name: cc.Label,      //player1 名字
        player1Owner: cc.Label,     //player1 房主
        player1gameID: cc.Label,    //player1 游戏ID
        player1staus: cc.Label,     //player1 状态 
        player1cardNum: cc.Label,   //玩家牌数
        player1douziNum: cc.Label,
        player2Avatar: cc.Sprite,   
        player2Name: cc.Label,
        player2Owner: cc.Label,
        player2gameID: cc.Label,
        player2status: cc.Label,
        player2cardNum: cc.Label,
        player2douziNum: cc.Label,
        player3Avatar: cc.Sprite,
        player3Name: cc.Label,
        player3Owner: cc.Label,
        player3gameID: cc.Label,
        player3status: cc.Label,
        player3cardNum: cc.Label,
        player3douziNum: cc.Label,


        leaveRoomButton: cc.Button,
        pukepaiEar: {                      //通过这个方法对发牌脚本进行调用
            default: null,
            type: B,
        },

        //通过改变按钮的Sting 来达到不同的操作，而不需要使用新的按钮
        caozuoButton: cc.Node,          //叫地主，抢地主等的节点
        readyLabel: cc.Label,           //准备按钮
        leftLabel: cc.Label,            //左边按钮
        rightLabel: cc.Label,           //右边按钮

        beishuLabel: cc.Label,

        audioControl: {
            default: null,
            type: cc.Node,
        },

        textButton: cc.Button,
        text2Button: cc.Button,

        timeLabel: cc.Label,
        gameTime: cc.Node,
    },

    onLoad () {
        this.gameTime.active = false;

        audio = this.audioControl.getComponent('audio');
        this.initNodeEvent();
        this.initMatchvsEVent(this);

        playerData.gamePlayer.beishu = 2;
        

        dizhupaiEar = cc.find('Canvas/dizhupai');
        cc.find('Canvas/caozuoButton');
        caozuoChildrenButton = this.caozuoButton.getChildren(); //得到子组件
        caozuoChildrenButton[0].active = false;
        caozuoChildrenButton[1].active = false;
        caozuoChildrenButton[2].active = false;
        caozuoChildrenButton[1].on('click', this.ready,this);
        caozuoChildrenButton[0].on('click', this.cancel, this);
        caozuoChildrenButton[2].on('click', this.enter, this);

        this.player1.active = false;
        this.player2.active = false
        this.player3.active = false;
        this.player1Owner.string = '房主';
        this.player2Owner.string = '';
        this.player3Owner.active = '';
        this.player1gameID.string = playerData.presentPlayer.gameID;
        this.player1douziNum.string = playerData.presentPlayer.douziNum;

        //对于任何玩家 player1ID 就是自身的 gameID 
        //player2ID 是 右手玩家ID ， player3ID 是 左手玩家ID
        playerData.gamePlayer.player1ID = playerData.presentPlayer.gameID;
        playerData.gamePlayer.player1douziNum = playerData.presentPlayer.douziNum;

        this.P1 = false;  //用来控制玩家1的显示
        this.p2 = false;  //用来控制玩家2的显示
        this.p3 = false;  //用来控制玩家3的显示

        if(gameData.roomInfo.roomID) {
            this.roomId.string = gameData.roomInfo.roomID;
         }   
        if(gameData.roomInfo.roomName) {
            this.roomName.string = gameData.roomInfo.roomName;
        }else {
            this.roomName.string = "随机房间";
        }
        
        
        
        this.setPlayer1();

        this.player1.active = true;

        this.textButton.node.on('click',function () {this.xipai();this.initDizhupai(); this.initpai();}, this);
        this.text2Button.node.on('click',function () {audio.playAudio(playerData.presentPlayer.sex,0); }, this);
    },

    update (dt) {
        //时间剩余
        if(this.gameTime.active) {
            if(remainTime <= 0 ) {
                this.timeLabel.string = 0;
            }else {
                if(timeIndex >= 1) {
                    dt -= 1;
                    remainTime -= timeIndex;
                    remainTime = Math.round(remainTime);
                    this.timeLabel.string = remainTime;
                    console.log(remainTime);
                }
            }
            timeIndex += dt;
        }
        
        
        

        this.beishuLabel.string = playerData.gamePlayer.beishu;

        //如果房间有三个玩家 则开始洗牌
        if(playerData.gamePlayer.playerNumber == 3 ) {
            if(!ready) { //用于控制它停止，让它只执行一次
                caozuoChildrenButton[1].active = true; //显示准备按钮
                this.xipai(); //洗牌
                ready = true; //
            }
        }else {
            caozuoChildrenButton[1].active = false;
        }

        //如果三个玩家都准备好了，开始发牌
        if(playerData.gamePlayer.player1IsReady && playerData.gamePlayer.player2IsReady && playerData.gamePlayer.player3IsReady){
            caozuoChildrenButton[1].active = false;  //隐藏准备按钮
            if(playerData.gamePlayer.ownerID == playerData.presentPlayer.gameID) { 
                //如果房主ID == 当前玩家ID  房主优先执行操作， 先显示房主的操作按钮，叫地主还是不叫地主
                caozuoChildrenButton[0].active = true;
                caozuoChildrenButton[2].active = true;
                this.gameTime.active = true;//倒计时显示出来
                remainTime = 25;//倒计时的时间
            }
            //重置 玩家ready，农民地主，牌数
            playerData.gamePlayer.player1IsReady = false;
            playerData.gamePlayer.player2IsReady = false;
            playerData.gamePlayer.player3IsReady = false;
            this.player1staus.string = '';
            this.player2status.string = '';
            this.player3status.string = '';
            this.player1cardNum.string = 17;
            this.player2cardNum.string = 17;
            this.player3cardNum.string = 17;
            this.fapai();      //发牌 
            this.initDizhupai();//将地主牌反面显示出来

        }
        //如果有个玩家没有叫地主，那么在抢地主的时候就需要跳过他，如何操作
        //如果有两个玩家没有叫地主，又该如何操作
        //这条件的顺序居然会有影响， mmp。。。。playerjiaodizhi 要放在前面
        if((playerData.gamePlayer.player1jiaodizhu && playerData.gamePlayer.player3jiaodizhi)||
        (playerData.gamePlayer.player1jiaodizhu && playerData.gamePlayer.player2jiaodizhu)) {
            this.leftLabel.string = '不抢';
            this.rightLabel.string = '抢地主';
            playerData.gamePlayer.player2jiaodizhu = false;
            playerData.gamePlayer.player3jiaodizhi = false;
        }
        //应该使用 pjiaodizhuStatus 进行判断
        //如果当前玩家叫地主， 其他玩家没叫
        if(playerData.gamePlayer.player1jiaodizhu && (playerData.gamePlayer.p2jiaodizhuStatus == 2 && playerData.gamePlayer.p3jiaodizhuStatus == 2)) {
            this.leftLabel.string = '不出';
            this.rightLabel.string = '出牌';
            this.player1staus.string = '地主';
            playerData.gamePlayer.p2jiaodizhuStatus = 0;
            playerData.gamePlayer.p3jiaodizhuStatus = 0;
            engine.prototype.sendEvent('显示');
            this.displayerDizhupai();//显示地主牌
            this.addCard(oldArr, playerData.gamePlayer.initPai); //给地主的牌里添加牌
            this.player1cardNum.string = 20;                //设置地主的牌的数量

        }

        
    },
    start () {
        for(var i = 1; i< 55; i++) {
            this.playerCardDisInst(i,i);
        }
        jiesuan.initAltals();
        audio.playBackground(0, 0.5);
    },

    

    /**
     * 按钮事件的监听
     */
    initNodeEvent: function () {
        this.leaveRoomButton.node.on('click', this.leaveRoom, this);

        this.node.on('sendPukepaiMessage',this.pukepaiMessage,this);
        //this.node.on('player1pai', function (arr) {oldArr = arr;});
        this.node.on('player2pai', function (arr) {playerData.gamePlayer.player2pai = arr;}, this);
        this.node.on('player3pai', function (arr) {playerData.gamePlayer.player3pai = arr;}, this);
        this.node.on('dizhupai', function (arr) {playerData.gamePlayer.dizhupaiArr = arr;}, this);
        this.node.on('sendSitdownMessage', this.sitdownMessage, this);
    },

    /**
     * Response回调监听
     * @param {this} self 
     */
    initMatchvsEVent(self) {
        response.prototype.bind();
        response.prototype.fightInit(self);
        this.node.on(msg.MATCHVS_JOINROOMRESPONSE,this.joinRoomResponse,this);
        this.node.on(msg.MATCHVS_JOINROOMNOTIFY,this.joinRoomNotify,this);
        this.node.on(msg.MATCHVS_SENDEVENTRESPONSE, this.sendEventResponse, this);
        this.node.on(msg.MATCHVS_SENDEVENTNOTIFY, this.sendEventNotify, this);
        this.node.on(msg.MATCHVS_LEAVEROOMRESPONSE, this.leaveRoomResponse, this);
        this.node.on(msg.MATCHVS_LEAVEROOMNOTIFY, this.leaveRoomNotify, this);

        this.node.on('newgame',this.newGame, this);
    },

    removeEvent () {

    },

    setPlayer1: function () {
        this.player1Name.string = playerData.presentPlayer.name;
        var player1Avatar = this.player1Avatar;
        playerData.gamePlayer.playerNumber++;
        cc.loader.loadRes(playerData.presentPlayer.avatar, cc.SpriteFrame, function (err,sp) {
            if(err){
                cc.error(err.message);
            }
            player1Avatar.spriteFrame = sp;
        });
    },

    /**
     * rightButton 点击事件
     * 叫地主 [1,0,0,1,ID,sex,事件序列]
     * 抢地主 [1,1,1,0,ID,sex,事件序列] 只要抢地主你就是地主，所以第三个为1， 不现实下一玩家操作按牛， 第四个为 0
     */
    enter: function () {
        console.log('点击右边');
        if(this.rightLabel.string == "叫地主") {
            this.gameTime.active = false;
            playerData.setDizhuMsg = [1,0,0,1,playerData.presentPlayer.gameID,playerData.presentPlayer.sex,101];
            audio.playAudio(playerData.presentPlayer.sex,101);
            var message = this.get1String(playerData.setDizhuMsg);      //一维数组转字符串
            var result = engine.prototype.sendEvent(message);
            console.log('jiaodizhu result:' + result.result);
            this.leftLabel.string ='';
            this.rightLabel.string = '';
            caozuoChildrenButton[2].active = false;                     //点击之后将按钮隐藏
            caozuoChildrenButton[0].active = false;
            playerData.gamePlayer.player1jiaodizhu = true;               //player1 是否叫地主
            playerData.gamePlayer.p1jiaodizhuStatus = 1;
            playerData.gamePlayer.beishu *= 2;
        }

        if(this.rightLabel.string == "抢地主") {
            this.gameTime.active = true;
            playerData.setDizhuMsg = [1,1,1,0,playerData.presentPlayer.gameID, playerData.presentPlayer.sex,0];
            audio.playAudio(playerData.presentPlayer.sex, 103);
            var message = this.get1String(playerData.setDizhuMsg);
            var result = engine.prototype.sendEvent(message);
            console.log('qiangdizhu result:' + result.result);
            playerData.gamePlayer.player1qiangdizhu = true;

            caozuoChildrenButton[0].active = false;
            caozuoChildrenButton[2].active = true;
            this.leftLabel.string = '不出';
            this.rightLabel.string = '出牌';
            this.player1staus.string = '地主';
            this.player2status.string = '';
            this.player3status.string = '';
            playerData.gamePlayer.beishu *= 2;
            
            
            engine.prototype.sendEvent("显示");             //发送消息，其他玩家显示地主牌 翻转
           
            this.displayerDizhupai();                       //显示地主的三张牌 翻转效果
            this.addCard(oldArr, playerData.gamePlayer.initPai); //给地主的牌里添加牌
            this.player1cardNum.string = 20;                //设置地主的牌的数量
            
            
        }

        if(this.rightLabel.string == "出牌") {
            this.gameTime.active = false;
            var result = checkCard.prototype.check(clickCard,otherCard,playerData.presentPlayer.sex);
            var num = playerData.gamePlayer.player1pai.length;
            if(result == 0) {
                console.log("合理出牌");
                //cc.find('Canvas/displayerCard').removeAllChildren(true);
                this.delCard(clickCard, playerData.gamePlayer.player1pai);
               
                num = num - clickCard.length;
                for(var i in clickCard) {
                    clickCard[i][6] = num;
                }
                var message = this.getString(clickCard);
                var resultMes = engine.prototype.sendEvent(message);
                console.log("出牌信息" + resultMes.result);
                //clickCard.splice(0,clickCard.length);
                // clickCard = [];
                otherCard = [];
                this.player1cardNum.string = num;
                caozuoChildrenButton[0].active = false;
                caozuoChildrenButton[2].active = false;
                if(num == 0) {
                    
                    if(this.player1staus.string == "地主") {
                        jiesuan.jiesuanUI('地主',1,playerData.presentPlayer.sex);     //传入是农民还地主， 胜利还是失败， 1 胜利，2 失败
                        var result = engine.prototype.sendEvent('地主结算');
                        console.log('地主结算消息 result' + result.result);
                    } else {
                        jiesuan.jiesuanUI('农民',1,playerData.presentPlayer.sex);     //自己打完牌不管是地主还是农民都是胜利的
                        var result = engine.prototype.sendEvent('农民结算');
                        console.log('农民结算消息 result' + result.result);
                    }
                   
                }else {
                    var resultNex = engine.prototype.sendEvent('出牌');
                    console.log('下家出牌' + resultNex.result);
                }
                
            }else if (result == 1) {
                console.log("非法出牌");
                audio.playAudio(playerData.presentPlayer.sex, 106);
            }

           
        }
    },

    /**
     * leftButton 点击事件
     * 不叫地主 [0,0,0,1,ID,sex,事件序列]
     * 不抢地主 [1,0,0,1,ID,sex,事件序列] 抢地主要先枪地主，所以第一个是 1
     */
    cancel: function () {
        if(this.leftLabel.string == "不叫") {
            this.gameTime.active = false;
            //playerData中定义了一个数组，用来存储 是否叫地主，是否抢地主，地主，下一个，ID，性别,事件 [0,0,0,0,0,0,0]
            playerData.setDizhuMsg = [0, 0, 0, 1,playerData.presentPlayer.gameID, playerData.presentPlayer.sex,1];
            audio.playAudio(playerData.presentPlayer.sex, 102);//播放不叫地主的音效
            var message = this.get1String(playerData.setDizhuMsg);//将一维数组信息转换成 字符串
            var result = engine.prototype.sendEvent(message);//发送消息
            console.log('bujiao result:' + result.result);
            caozuoChildrenButton[2].active = false;//隐藏按钮
            caozuoChildrenButton[0].active = false;
            playerData.gamePlayer.player1jiaodizhu = false;
            playerData.gamePlayer.p1jiaodizhuStatus = 2;
            this.leftLabel.string ='';
            this.rightLabel.string = '';
        }
        if(this.leftLabel.string == "不抢") {
            this.gameTime.active = false;
            playerData.setDizhuMsg = [1, 0, 0, 1,playerData.presentPlayer.gameID, playerData.presentPlayer.sex,3];
            audio.playAudio(playerData.presentPlayer.sex, 104);
            var message = this.get1String(playerData.setDizhuMsg);
            var result = engine.prototype.sendEvent(message);
            console.log('buqiang result:' + result.result);
            playerData.gamePlayer.player1qiangdizhu = false;

            caozuoChildrenButton[2].active = false;
            caozuoChildrenButton[0].active = false;
            playerData.gamePlayer.player1qiangdizhu = false;
            this.leftLabel.string ='';
            this.rightLabel.string = '';
            this.player1staus.string = '';

        }

        if(this.leftLabel.string == "不出") {
            this.gameTime.active = false;
            for(var i in otherCard) {
                otherCard[i][5] = 2;
                //otherCard[i][6] = playerData.gamePlayer.player1pai.length;
            }
            var message = this.getString(otherCard);
            var resultMes = engine.prototype.sendEvent(message);
            audio.playAudio(playerData.presentPlayer.sex, 105)
            console.log("上家出牌信息" + resultMes.result);

            var resultNex = engine.prototype.sendEvent('出牌');
            playerData.setDizhuMsg = [2, 2, 2, 2,playerData.presentPlayer.gameID, playerData.presentPlayer.sex,3];
            var message = this.get1String(playerData.setDizhuMsg);
            var result = engine.prototype.sendEvent(message);
            console.log('下家出牌' + resultNex.result);

            var message = this.getString(otherCard);
            var resultMes = engine.prototype.sendEvent(message);
            console.log("出牌信息" + resultMes.result);
            caozuoChildrenButton[2].active = false;
            caozuoChildrenButton[0].active = false;
            clickCard = [];
            recordClickCard = [];
            
        }
    },

    /**
     * readyButton 点击事件
     */
    ready: function () {
        //定义了一个对象caozuoMsg，里面包括属性  是否准备，是否叫地主，是否抢地主，是否出牌
      if(caozuoMsg.isReady) {
          caozuoMsg.isReady = false;
          this.readyLabel.string = "准备";
          playerData.gamePlayer.player1IsReady = false;
          var result = engine.prototype.sendEvent('取消准备');//发消息通知其他玩家
      } else {
          caozuoMsg.isReady = true;
          this.readyLabel.string = "取消准备";
          playerData.gamePlayer.player1IsReady = true;
          var result = engine.prototype.sendEvent('准备');//发消息通知其他玩家
      }
    },

   //洗牌
    xipai: function () {
        //由房主洗牌
        if(playerData.gamePlayer.ownerID == playerData.presentPlayer.gameID) {
            this.pukepaiEar.initArray();//生成一副牌，二维数组，二维中的0，1，2，3等用来存储不同的信息，
            this.pukepaiEar.shuffle(playerData.gamePlayer.paiArray);//打乱牌，洗牌
            this.pukepaiEar.fapai(playerData.gamePlayer.paiArray);//发牌
            for(var i = 0; i< 17; i++) {
                //在每张排后面加上各自玩家的 ID ， 用于识别牌应该发送的玩家
                playerData.gamePlayer.player1pai[i][3] = playerData.gamePlayer.player1ID;
                playerData.gamePlayer.player2pai[i][3] = playerData.gamePlayer.player2ID;
                playerData.gamePlayer.player3pai[i][3] = playerData.gamePlayer.player3ID;
                //添加性别， 初始化为0， 发牌后由玩家再设置
                playerData.gamePlayer.player1pai[i][4] = 0;
                playerData.gamePlayer.player2pai[i][4] = 0;
                playerData.gamePlayer.player3pai[i][4] = 0;
                //添加是发牌还是出牌
                playerData.gamePlayer.player1pai[i][5] = 0;
                playerData.gamePlayer.player2pai[i][5] = 0;
                playerData.gamePlayer.player3pai[i][5] = 0;

                playerData.gamePlayer.player1pai[i][6] = 0;
                playerData.gamePlayer.player2pai[i][6] = 0;
                playerData.gamePlayer.player3pai[i][6] = 0;
            }
            this.pukepaiEar.paixu(playerData.gamePlayer.player1pai,2);//将发到的牌排序
            this.pukepaiEar.setPlayerCard(playerData.gamePlayer.player1pai);//将玩家的牌 赋值给了一个变量 playerCard
            this.pukepaiEar.text(playerData.gamePlayer.player1pai);  //这个是牌的实例化
            for(var k in playerData.gamePlayer.dizhupaiArr) {//对地主牌的设置，K 是个变量，用来存储属性名。遍历数组
                playerData.gamePlayer.dizhupaiArr[k][3] = 1000;
                playerData.gamePlayer.dizhupaiArr[k][4] = 0;
                playerData.gamePlayer.dizhupaiArr[k][5] = 0;
                playerData.gamePlayer.dizhupaiArr[k][6] = 0;
            }
            var player2cardNum = this.getString(playerData.gamePlayer.player2pai); //对二维数组 字符串化，数组代表牌
            var player3cardNum = this.getString(playerData.gamePlayer.player3pai);
            var dizhupaicardNum = this.getString(playerData.gamePlayer.dizhupaiArr);
            var result2 = engine.prototype.sendEvent(player2cardNum); //sendEvent 发送的是Strig 字符串
            var result3 = engine.prototype.sendEvent(player3cardNum);//将玩家2和玩家3的牌还有地主牌 的信息 发送给其他玩家
            var result3 = engine.prototype.sendEvent(dizhupaicardNum);
            console.log("player2pai sendEventEx result:" + result2.result);
            console.log("player3pai sendEventEx result:" + result3.result);
            this.initpai();   //发牌的同时也对地主三张牌初始化
            oldArr = playerData.gamePlayer.player1pai;
            var str = this.getString(oldArr);//getString();将二维数组转换成字符串
            oldArr = this.getArray(str);//getArray();将字符串转换成数组 
        } 
    },

    /**
     * 发牌
     */
   fapai: function() {
        //由房主发牌，在洗牌的时候房间的玩家就收到了 各自的牌
        if(playerData.gamePlayer.ownerID == playerData.presentPlayer.gameID) {
            console.log(playerData.gamePlayer.ownerID);
            console.log(playerData.presentPlayer.gameID);
            this.pukepaiEar.text2(playerData.gamePlayer.initPai);
            var result = engine.prototype.sendEvent("发牌"); //房间玩家在收到这条信息的时候进行发牌
            console.log("发牌 result" + result);
        }
    },

    /**
     * 显示牌的反面，在发牌之后
     */
    initDizhupai: function () {
        
        var actionLeft = cc.moveTo(0.2,cc.v2(-60,0));
        var actionRigh = cc.moveTo(0.2,cc.v2(60,0));
        cardLeft = cc.find('Canvas/dizhupai/cardLeft');
        cardRight = cc.find('Canvas/dizhupai/cardRight');
        cardCenter = cc.find('Canvas/dizhupai/cardCenter');
        cardLeft.runAction(actionLeft);
        cardRight.runAction(actionRigh);
        
    },

    /**
     * 查找那三张牌并调用 实例化函数实例化
     */
    initpai: function () {
        for(var i=0; i<playerData.gamePlayer.dizhupaiArr.length; i++) {
            
            this.pukepaiEar.paixu(playerData.gamePlayer.dizhupaiArr,2);
            var weizhi = playerData.gamePlayer.dizhupaiArr[i][0];
            this.initdizhupai2(weizhi,i);
           
        }
        console.log('地主的牌');
        console.log(playerData.gamePlayer.dizhupaiArr);
    },

    //实例化 三张牌
    initdizhupai2: function (weizhi,i) {
        cc.loader.loadRes("prefab/pukepai_" + weizhi, cc.Prefab, function(err, prefab) {
            if(err) {
                cc.error(err);
                return;
            }
            
            playerData.gamePlayer.addDizhupai[i] = cc.instantiate(prefab);    //克隆card 用新的Array记录，用于将它插入到玩家牌中
            playerData.gamePlayer.addDizhupai[i].status = SITDOWN;           //不能直接受用playerData.gamePlayer.dizhupai , 会出现重复addChild Error
            playerData.gamePlayer.addDizhupai[i].isChoise = false;

            var card = cc.instantiate(prefab);
            card.status = SITDOWN;
            card.isChoise = false;
            playerData.gamePlayer.dizhupai[i] = card;
        })
    },

    /**
     * 显示三张牌
     */
    displayerDizhupai: function () {
       var actionRotation = cc.rotateTo(0.2,0,-90);
       var actionRotaion2 = cc.rotateTo(0.2,0,-90);
        cardLeft.runAction(actionRotation);
        cardCenter.runAction(actionRotaion2);
        var actionRotation3 = cc.rotateTo(0.2,0,-90);
        cardRight.runAction(actionRotation3);

        dizhupaiEar.removeAllChildren(true);
        for(var i=0; i< playerData.gamePlayer.dizhupaiArr.length; i++) {
            dizhupaiEar.addChild(playerData.gamePlayer.dizhupai[i]);
            //playerData.gamePlayer.initPai[i].setContentSize(cc.size(55,70)); //控制size, 由于初始牌的设计没做好。不能用Size去控制大小
            playerData.gamePlayer.dizhupai[i].scale = 0.38;   //控制牌的scale
            playerData.gamePlayer.dizhupai[i].position = cc.v2(-60 + 60*i,0);
            playerData.gamePlayer.dizhupai[i].rotationY = 90;
            playerData.gamePlayer.dizhupai[i].runAction(cc.rotateTo(0.2,0,0));
        }
    },




    /**
     * 将二维数组转为 String
     * @param {数组} objarr 
     */
    getString: function ( objarr ){
        　　var typeNO = objarr.length;
          　var tree = "[";
         　for (var i = 0 ;i < typeNO ; i++){
           　　 tree += "[";
           　　 tree += objarr[i][0]+",";
           　　 tree += objarr[i][1]+",";
                tree += objarr[i][2]+",";
                tree += objarr[i][3]+ ",";
                tree += objarr[i][4]+ ",";
                tree +=objarr[i][5]+ ",";
                tree += objarr[i][6];
          　　  tree += "]";
          　　　if(i<typeNO-1){
            　　 　tree+=",";
         　　　 }
          　}
          　tree+="]";
          　return tree;
    },

    /**
     * 将一维数组转为 string
     * @param {arr} objarr 
     */
    get1String: function (objarr) {
        var typeNO = objarr.length;
        var tree = "[";
        for(var i =0; i< typeNO; i++) {
            tree += objarr[i] ;
            if (i<typeNO-1) {
                tree += ","; 
            }
        }
        tree += "]";
        return tree;
    },

    /**
     * 将字符串转化为数组 ， 这个字符串必须是数组的样子
     * @param {string} objstring 
     */
    getArray: function (objstring) {
        var str = eval(objstring);
        return str;
    },

    /**
     * 
     * @param {点击的牌} message 
     */
    pukepaiMessage: function (message) {
        console.log(message);
        clickCard.push(message);
        recordClickCard = clickCard;
        this.pukepaiEar.paixu(clickCard,2);
    },

    /**
     * 
     * @param {放下的牌} message 
     */
    sitdownMessage: function (message) {
        console.log(message);
        var index = clickCard.indexOf(message);
        if(index >= 0) {
            clickCard.splice(index,1);
            recordClickCard = clickCard;
        }
    },

    sendEventResponse: function (rsp) {
        if(rsp.status == 200) {
            console.log("信息发送成功");
        } else {
            console.log("sendEventResposse错误：" + rsp.status);
        }
    },

    /**
     * 对收到的信息进行判断
     * @param {收到的信息} eventInfo 
     */
    sendEventNotify: function (eventInfo) {
        console.log(eventInfo);
        //如果发送消息的是 player2ID（相对于自己） 那么对 player2 的信息进行更改
        if (eventInfo.srcUserID == playerData.gamePlayer.player2ID) {
            if(eventInfo.cpProto == "准备"){
                this.player2status.string = "准备";
                playerData.gamePlayer.player2IsReady = true;
            }else if (eventInfo.cpProto == "取消准备") {
                this.player2status.string = "";
                playerData.gamePlayer.player2IsReady = false;
            }else if(eventInfo.cpProto == "地主结算") {

                jiesuan.jiesuanUI('农民',2);
            }else if(eventInfo.cpProto == "农民结算") {
                if(this.player1staus.string == '地主') {
                    jiesuan.jiesuanUI('地主',2,playerData.presentPlayer.sex)
                }else {
                    jiesuan.jiesuanUI('农民',1,playerData.presentPlayer.sex);
                }
            }else if(eventInfo.cpProto == "newgame") {
                playerData.gamePlayer.playerNumber ++;
            }
        }
        //如果发送消息的是 player2ID（相对于自己） 那么对 player2 的信息进行更改
        if (eventInfo.srcUserID == playerData.gamePlayer.player3ID) {
            if(eventInfo.cpProto == "准备"){
                this.player3status.string = "准备";
                playerData.gamePlayer.player3IsReady = true;
            }else if (eventInfo.cpProto == "取消准备") {
                this.player3status.string = '';
                playerData.gamePlayer.player3IsReady = false;
            }else if (eventInfo.cpProto == "出牌") {
                this.gameTime.active = true;
                remainTime = 25;
                this.leftLabel.string = "不出";
                this.rightLabel.string = "出牌";
                caozuoChildrenButton[0].active = true;
                caozuoChildrenButton[2].active = true;
            }else if(eventInfo.cpProto == "地主结算") {
                jiesuan.jiesuanUI('农民',2,playerData.presentPlayer.sex);
            }else if(eventInfo.cpProto == "农民结算") {
                if(this.player1staus.string == '地主') {
                    jiesuan.jiesuanUI('地主',2,playerData.presentPlayer.sex)
                }else {
                    jiesuan.jiesuanUI('农民',1,playerData.presentPlayer.sex);
                }
            }else if(eventInfo.cpProto == "newgame") {
                playerData.gamePlayer.playerNumber++;
            }
        }
       
        if(eventInfo.cpProto != "准备" && eventInfo.cpProto != "取消准备" && eventInfo.cpProto != "发牌" 
        && eventInfo.cpProto != "显示" && eventInfo.cpProto != "出牌" && eventInfo.cpProto != "地主结算"
        && eventInfo.cpProto != "农民结算" && eventInfo.cpProto != "newgame" ) {
            cardArray = this.getArray(eventInfo.cpProto);

           if(cardArray.length != 0) {
                if(cardArray[0][5] == 0) {
                var selfCard = true;
                console.log(cardArray);
                for(var i in cardArray) {
                    if (cardArray[i][3]){
                        if(cardArray[i][3] == playerData.presentPlayer.gameID){
                        }else {
                            selfCard = false;
                        }
                    }else {
                        selfCard = false;
                    }
                }
                if(selfCard) {
                    playerData.gamePlayer.player1pai = cardArray;  //在收到牌的数据后，赋值给player1pai
                                                                    //发送过来的牌是无序的
                    this.pukepaiEar.paixu(cardArray,2);             //对牌进行排序
                    oldArr = cardArray;                 //oldArr 必须进行排序，不然在 addCard 时会出错
                    var str = this.getString(oldArr);
                    oldArr = this.getArray(str);
                    this.pukepaiEar.setPlayerCard(cardArray);       //将牌的信息传给 pukepaiEar 脚本
                    this.pukepaiEar.text(cardArray);
                    
                    }
                    if(cardArray.length == 3 && cardArray[1][3] == 1000) {                         //如果这个数组的长度为3，则表示是地主的三张牌
                        playerData.gamePlayer.dizhupaiArr = cardArray;  //对dizhupaiArr进行赋值
                        this.initpai();                              //对得到的三张牌的数据进行初始化
    
                    }
                }
                if(cardArray[0][5] == 1) {          //1 说明是出的牌
                    otherCard = [];
                    checkCard.prototype.check(cardArray, otherCard,cardArray[0][4]);
                    if(eventInfo.srcUserID == playerData.gamePlayer.player3ID) {     //
                        playerData.gamePlayer.player3SendCard = cardArray;
                        console.log('玩家出的牌');
                        console.log(playerData.gamePlayer.player3SendCard);
                        var num = cardArray[0][6];
                        this.player3cardNum.string = num;
                        //cc.find('Canvas/player3DisCard').removeAllChildren();
                        this.player3CardDis(cardArray);
                        cc.find('Canvas/displayerCard').removeAllChildren(true);  //清理显示的牌
                       
                        otherCard = cardArray;
                        clickCard = [];
                    }else if(eventInfo.srcUserID == playerData.gamePlayer.player2ID) {
                        playerData.gamePlayer.player2SendCard = cardArray;
                        console.log('玩家出的牌');
                        console.log(playerData.gamePlayer.player2SendCard);
                        this.player2cardNum.string = cardArray[0][6];
                        this.player2CardDis(cardArray);
                        cc.find('Canvas/displayerCard').removeAllChildren(true);  //清理显示的牌
                        
                        // for(var i in cardArray) {
                        //     var weizhi = cardArray[i][0];
                        //     this.player2CardDis(weizhi,i);
                        // }
                    }
                }
                if(cardArray[0][5] == 2) {
                   
                    if(eventInfo.srcUserID == playerData.gamePlayer.player3ID) { 
                         console.log('上一个玩家出的牌')
                        console.log(cardArray);
                        for(var i in cardArray) {
                            cardArray[i][5] = 1;
                            //cardArray[i][6] = playerData.gamePlayer.player1pai.length;
                        }
                        otherCard = cardArray;
                        playerData.gamePlayer.player3SendCard = cardArray;
                        cc.find('Canvas/displayerCard').removeAllChildren(true);
                        cc.find('Canvas/player3DisCard').removeAllChildren(true);
                        for(var i in cardArray) {
                            if(recordClickCard != 0 ) {
                                if(recordClickCard[i][0] == cardArray[i][0]) {
                                    console.log('清空otehrCard');
                                    console.log(cardArray[i]);
                                    console.log(clickCard[i]);
                                    otherCard = [];
                                    caozuoChildrenButton[0].active = false;
                                }
                                
                            }
                        }
                        
                        clickCard = [];
                        //var num = cardArray[0][6];
                        // this.play
                    }else if(eventInfo.srcUserID == playerData.gamePlayer.player2ID) {
                        cc.find('Canvas/player2DisCard').removeAllChildren(true);
                    }
                }
           }
 



            //叫地主，抢地主操作
            //操作根据玩家顺序来执行， 房主优先， 然后2号， 最后3号， 返回房主
            //对于任何一位玩家， player1 都是自身， 右手边都是player2 ，左手 player3
            //任何玩家的 消息都是发送给所有玩家， 收到消息的玩家通过ID来判断是谁发的消息， 并做出相应操做
            //1叫地主->2叫地主 ->3叫地主 ->1抢地主 那么1就是地主 ， 谁抢地主谁就是地主
            //如果 房主不叫地主， 2，3叫地主， 那么在抢地主时需要跳过房主 ，对于其他玩家也一样
            //如果 一个玩家叫地主，另外两个不叫， 地主是叫地主玩家的
            //如果， 没有人叫地主，随机一个地主？ 还是重新洗牌？？
    
            //如果是左手方向的玩家发来的信息
            if(cardArray[4] == playerData.gamePlayer.player3ID) {
            
                if(cardArray[0] == 1 && cardArray[2] == 0) {
                    playerData.gamePlayer.player3jiaodizhi = true;
                    playerData.gamePlayer.p3jiaodizhuStatus = 1;
                    this.player3status.string = '叫地主';
                    playerData.gamePlayer.beishu *= 2;
                    audio.playAudio(cardArray[5],101);
                }else if(cardArray[0] == 0){
                    playerData.gamePlayer.player3jiaodizhi = false;
                    playerData.gamePlayer.p3jiaodizhuStatus = 2;
                    audio.playAudio(cardArray[5],102);
                }else if(cardArray[1] == 1 && cardArray[2] == 1) {
                    //playerData.gamePlayer.beishu *= 2;
                    this.player3status.string = '地主';
                    this.player2status.string = '';
                    audio.playAudio(cardArray[5],103);
                }else if(cardArray[1] == 1 && cardArray[2] != 1){
                    audio.playAudio(cardArray[5],104);
                }
                if(cardArray[2] == 1) {
                    this.player3cardNum.string = 20;
                }

                if(cardArray[3] == 1){    
                    this.gameTime.active = true;
                    remainTime = 25; 
                
                    caozuoChildrenButton[0].active = true;
                    caozuoChildrenButton[2].active = true;
                    console.log("显示按钮");
                    //
                    //p1jiaodizhuStatus 共有三个状态， 0：默认 1：叫地主 2：不叫地主
                    //跳过本玩家，检测本玩家有没有叫地主，没有就不显示按钮，并发送消息给下一个玩家
                    if(playerData.gamePlayer.p1jiaodizhuStatus == 2) {  //实现跳过没叫地主的玩家操作
                        caozuoChildrenButton[0].active = false;
                        caozuoChildrenButton[2].active = false;
                        playerData.setDizhuMsg = [0,0,0,1,playerData.presentPlayer.gameID, playerData.presentPlayer.sex,0];
                        var message = this.get1String(playerData.setDizhuMsg);
                        var result = engine.prototype.sendEvent(message);
                        console.log('跳过不叫地主的玩家' + result.result);
                    }     
                 }else if (cardArray[3] == 2) {
                     audio.playAudio(cardArray[5], 105);
                 }
            }

            //如果是右手方向玩家发来的消息
            if(cardArray[4] == playerData.gamePlayer.player2ID) {
                if(cardArray[0] == 1 && cardArray[2] == 0) {
                    playerData.gamePlayer.player2jiaodizhu = true;
                    playerData.gamePlayer.p2jiaodizhuStatus = 1;
                    this.player2status.string = '叫地主';
                    playerData.gamePlayer.beishu *= 2;
                    audio.playAudio(cardArray[5],101);
                }else if(cardArray[0] == 0){
                    playerData.gamePlayer.player2jiaodizhi = false;
                    playerData.gamePlayer.p2jiaodizhuStatus = 2;
                    audio.playAudio(cardArray[5],102);
                }else if(cardArray[1] == 1 && cardArray[2] == 1) {
                    //playerData.gamePlayer.beishu *= 2;
                    this.player2status.string = '地主';
                    this.player3status.string = '';
                    audio.playAudio(cardArray[5],103);
                }else if(cardArray[1] == 1 && cardArray[2] == 0){
                    audio.playAudio(cardArray[5],104);
                }
                if(cardArray[2] == 1) {
                    this.player2cardNum.string = 20;
                }else if (cardArray[3] == 2) {
                    audio.playAudio(cardArray[5], 105);
                }
            }
            
            
        }
        cardArray = null;
        if(eventInfo.cpProto == "发牌") {
                this.pukepaiEar.text2(playerData.gamePlayer.initPai);
        }
        if(eventInfo.cpProto == "显示") {
            this.displayerDizhupai();
        }
        
    },


    newGame: function () {
        console.log('newGame');
        caozuoChildrenButton[1].active = true;
        cc.find('Canvas/pukepaiEar').removeAllChildren(true);
        cc.find('Canvas/player2DisCard').removeAllChildren(true);
        cc.find('Canvas/player3DisCard').removeAllChildren(true);
        dizhupaiEar.removeAllChildren(true);
        cc.find('Canvas/displayerCard').removeAllChildren(true);
        this.readyLabel.string = "准备";
        this.leftLabel.string = '';
        this.rightLabel.string = '';
        this.player1douziNum.string = '';
        this.player2douziNum.string = '';
        this.player3douziNum.string = '';
        this.beishuLabel.string = 2;
        playerData.gamePlayer.beishu = 2;
        playerData.gamePlayer.playerNumber = 1;
        var result = engine.prototype.sendEvent('newgame');
        console.log('newgame result:' + result.result);

        playerData.gamePlayer.playerSendCard = [];
        playerData.gamePlayer.player2SendCard = [];
        playerData.gamePlayer.player3SendCard = [];
        playerData.gamePlayer.addDizhupai = [];
        playerData.gamePlayer.paiArray = [];
        playerData.gamePlayer.dizhupai = [];
        playerData.gamePlayer.dizhupaiArr = [];
        playerData.gamePlayer.initPai = [];

        playerData.gamePlayer.player1IsReady = false;
        playerData.gamePlayer.player1pai = [];
        playerData.gamePlayer.player1jiaodizhu = false;
        playerData.gamePlayer.p1jiaodizhuStatus = 0;
        playerData.gamePlayer.player1qiangdizhu = false;

        playerData.gamePlayer.player2IsReady = false;
        playerData.gamePlayer.player2pai = [];
        playerData.gamePlayer.player2jiaodizhu = false;
        playerData.gamePlayer.p2jiaodizhuStatus = 0;
        playerData.gamePlayer.player2qiangdizhu = false;

        playerData.gamePlayer.player3IsReady = false;
        playerData.gamePlayer.player3pai = [];
        playerData.gamePlayer.player3jiaodizhu = false;
        playerData.gamePlayer.p3jiaodizhuStatus = 0;
        playerData.gamePlayer.player3qiangdizhu = false;

        caozuoMsg.isReady = false;
        caozuoMsg.isJiaodizhu = false;
        caozuoMsg.isQiangdizhu = false;
        caozuoMsg.isChupai = false;

        ready = false;
        cardArray = [];
        oldArr = [];
        newArr = [];
        clickCard = [];
        otherCard = [];
        displayCard = [];

        
    },

    leaveRoom: function () {
        var result = engine.prototype.leaveRoom(playerData.presentPlayer.name);
        console.log("leaveRoom Result:" + result);
        

    },

    leaveRoomResponse: function (rsp) {
        ready = false;
        audio.playBackground(1,0.5);
        console.log("leaveRoomResponse Result:" + rsp.status);
        if(rsp.status == 200) {
            this.player1Name.string = '';
            this.player1Owner.string = '';
            this.player1.active = false;
            //对playerDat.gamePlayer的数据进行重置
            this.beishuLabel.string = '';
            playerData.gamePlayer.beishu = 2;

            playerData.gamePlayer.playerSendCard = [];
            playerData.gamePlayer.player2SendCard = [];
            playerData.gamePlayer.player3SendCard = [];
            playerData.gamePlayer.addDizhupai = [];
            playerData.gamePlayer.paiArray = [];
            playerData.gamePlayer.dizhupai = [];
            playerData.gamePlayer.dizhupaiArr = [];
            playerData.gamePlayer.initPai = [];
            playerData.gamePlayer.ownerID = null;
            playerData.gamePlayer.playerNumber = 0;
            playerData.gamePlayer.player1ID = null;
            playerData.gamePlayer.player1IsReady = false;
            playerData.gamePlayer.player1pai = [];
            playerData.gamePlayer.player1jiaodizhu = false;
            playerData.gamePlayer.p1jiaodizhuStatus = 0;
            playerData.gamePlayer.player1qiangdizhu = false;

            playerData.gamePlayer.player2ID = null;
            playerData.gamePlayer.player2IsReady = false;
            playerData.gamePlayer.player2pai = [];
            playerData.gamePlayer.player2jiaodizhu = false;
            playerData.gamePlayer.p2jiaodizhuStatus = 0;
            playerData.gamePlayer.player2qiangdizhu = false;

            playerData.gamePlayer.player3ID = null;
            playerData.gamePlayer.player3IsReady = false;
            playerData.gamePlayer.player3pai = [];
            playerData.gamePlayer.player3jiaodizhu = false;
            playerData.gamePlayer.p3jiaodizhuStatus = 0;
            playerData.gamePlayer.player3qiangdizhu = false;

            caozuoMsg.isReady = false;
            caozuoMsg.isJiaodizhu = false;
            caozuoMsg.isQiangdizhu = false;
            caozuoMsg.isChupai = false;

            ready = false;
            cardArray = [];
            oldArr = [];
            newArr = [];
            clickCard = [];
            otherCard = [];
            displayCard = [];
            

            
        }
        cc.director.loadScene('main');
    },

    /**
     * 当玩家离开房间通知其他玩家
     * @param {离开房间的玩家信息} leaveRoomInfo 
     */
    leaveRoomNotify: function (leaveRoomInfo) {
        ready = false;             
        playerData.gamePlayer.ownerID = leaveRoomInfo.owner;
        this.readyLabel.string = "准备";
        caozuoMsg.isReady = false;
        console.log(leaveRoomInfo);
        if(leaveRoomInfo.userID == playerData.gamePlayer.player2ID) {
            this.player2Name.string = '';
            this.player2Owner.string = '';
            this.player2gameID.string = '';
            this.player2status.string = '';
            this.player2Avatar.spriteFrame = null;
            this.player2.active = false;
            this.p2 = false;
            playerData.gamePlayer.player2ID = '';
            playerData.gamePlayer.player2IsReady = false;
            playerData.gamePlayer.playerNumber--;
            if(leaveRoomInfo.owner == playerData.gamePlayer.player1ID) {
                this.player1Owner.string = '房主';
            }else {
                this.player3Owner.string = '房主';
            }
        }
        if(leaveRoomInfo.userID == playerData.gamePlayer.player3ID) {
            this.player3Name.string = '';
            this.player3Owner.string = '';
            this.player3gameID.string = '';
            this.player3status.string = '';
            this.player3Avatar.spriteFrame = null;
            this.player3.active = false;
            this.p3 = false;
            playerData.gamePlayer.player3ID = '';
            playerData.gamePlayer.player3IsReady = false;
            playerData.gamePlayer.playerNumber--;
            if(leaveRoomInfo.owner == playerData.gamePlayer.player1ID) {
                this.player1Owner.string = '房主';
            }else {
                this.player2Owner.string = '房主';
            }
        }
    },

    /**
     * 
     * @param {房间成员信息，不包括自己} roomUserInfoList 
     * @param {房间信息} roomInfo 
     * 玩家加入房间位置的逻辑
     */
    joinRoomResponse: function (roomUserInfoList, roomInfo) {
        ready = false;
        playerData.gamePlayer.ownerID = roomInfo.owner;
        //console.log(roomInfo.owner);
        this.roomId.string = roomInfo.roomID;
        for (var i = 0; i<roomUserInfoList.length; i++) {
            console.log(roomUserInfoList[i]);
            var playerMessage = this.findPlayerMessage(roomUserInfoList[i].userProfile);
            if(roomUserInfoList.length == 1){
                var player3Avatar = this.player3Avatar;
                    cc.loader.loadRes(playerMessage.avatar, cc.SpriteFrame, function (err,sp) {
                    if(err){
                        cc.error(err.message);
                    }
                    player3Avatar.spriteFrame = sp;
                    });
                    this.player3Name.string = playerMessage.name;
                    this.player1Owner.string = '';
                    this.player3Owner.string = '房主';
                    this.player3gameID.string = roomUserInfoList[i].userID;
                    playerData.gamePlayer.player3ID = roomUserInfoList[i].userID;
                    playerData.gamePlayer.playerNumber++;
                    this.player3douziNum.string = playerMessage.huanledouNumber;
                    playerData.gamePlayer.player3douzhiNum = playerMessage.huanledouNumber;
                    this.player3.active = true;
                    this.p3 = true;
            }
            if(roomUserInfoList.length == 2 && roomUserInfoList[0].userID == roomInfo.owner) {
                if(i == 0) {
                    //var player2Message = this.findPlayerMessage(roomUserInfoList[i].userProfile);
                    var player2Avatar = this.player2Avatar;
                    cc.loader.loadRes(playerMessage.avatar, cc.SpriteFrame, function (err,sp) {
                        if(err){
                            cc.error(err.message);
                         }
                        player2Avatar.spriteFrame = sp;
                        });
                    this.player2Name.string = playerMessage.name;
                    this.player1Owner.string = '';
                    this.player2Owner.string = '房主';
                    this.player2gameID.string = roomUserInfoList[i].userID;
                    playerData.gamePlayer.player2ID = roomUserInfoList[i].userID;
                    playerData.gamePlayer.playerNumber++;
                    this.player2douziNum.string = playerMessage.huanledouNumber;
                    playerData.gamePlayer.player2douzhiNum = playerMessage.huanledouNumber;
                    this.player2.active = true;
                    this.p2 = true;
                }
                if(i == 1) {
                    var player3Avatar = this.player3Avatar;
                    cc.loader.loadRes(playerMessage.avatar, cc.SpriteFrame, function (err,sp) {
                    if(err){
                        cc.error(err.message);
                    }
                    player3Avatar.spriteFrame = sp;
                    });
                    this.player3Name.string = playerMessage.name;
                    this.player3Owner.string = '';
                    this.player3gameID.string = roomUserInfoList[i].userID;
                    playerData.gamePlayer.player3ID = roomUserInfoList[i].userID;
                    playerData.gamePlayer.playerNumber++;
                    this.player3douziNum.string = playerMessage.huanledouNumber;
                    playerData.gamePlayer.player3douzhiNum = playerMessage.huanledouNumber
                    this.player3.active = true;
                    this.p3 = true;
                }
            }
            if(roomUserInfoList.length == 2 && roomUserInfoList[0].userID != roomInfo.owner) {
                if(i == 0) {
                    var player3Avatar = this.player3Avatar;
                    cc.loader.loadRes(playerMessage.avatar, cc.SpriteFrame, function (err,sp) {
                    if(err){
                        cc.error(err.message);
                    }
                    player3Avatar.spriteFrame = sp;
                    });
                    this.player3Name.string = playerMessage.name;
                    this.player3Owner.string = '';
                    this.player3gameID.string = roomUserInfoList[i].userID;
                    playerData.gamePlayer.player3ID = roomUserInfoList[i].userID;
                    playerData.gamePlayer.playerNumber++;
                    this.player3douziNum.string = playerMessage.huanledouNumber;
                    playerData.gamePlayer.player3douzhiNum = playerMessage.huanledouNumber
                    this.player3.active = true;
                    this.p3 = true;
                }
                if (i == 1 ){
                    var player2Avatar = this.player2Avatar;
                    cc.loader.loadRes(playerMessage.avatar, cc.SpriteFrame, function (err,sp) {
                        if(err){
                            cc.error(err.message);
                         }
                        player2Avatar.spriteFrame = sp;
                        });
                    this.player2Name.string = playerMessage.name;
                    this.player1Owner.string = '';
                    this.player2Owner.string = '房主';
                    this.player2gameID.string = roomUserInfoList[i].userID;
                    playerData.gamePlayer.player2ID = roomUserInfoList[i].userID;
                    playerData.gamePlayer.playerNumber++;
                    this.player2douziNum.string = playerMessage.huanledouNumber;
                    playerData.gamePlayer.player2douzhiNum = playerMessage.huanledouNumber
                    this.player2.active = true;
                    this.p2 = true;
                }
            }
        }
    },

    joinRoomNotify: function (roomUserInfo) {
        cc.log('其他人进入房间');
         if (this.p2 && !this.p3) {
            var player3Message = this.findPlayerMessage(roomUserInfo.userProfile);
            var player33Avatar = this.player3Avatar;
            cc.loader.loadRes(player3Message.avatar, cc.SpriteFrame, function (err,sp) {
                if(err){
                    cc.error(err.message);
                }
                player33Avatar.spriteFrame = sp;
            });
            this.player3Name.string = player3Message.name;
            this.player3gameID.string = roomUserInfo.userID;
            playerData.gamePlayer.player3ID = roomUserInfo.userID;
            playerData.gamePlayer.playerNumber++;
            this.player3douziNum.string = player3Message.huanledouNumber;
            playerData.gamePlayer.player3douzhiNum = player3Message.huanledouNumber
            this.player3Owner.string = '';
            this.player3.active = true;
            this.p3 = true;
            
        } 
        
        if(!this.p2 && this.p3) {
           var player2Message = this.findPlayerMessage(roomUserInfo.userProfile);
           var player222Avatar = this.player2Avatar;
           cc.loader.loadRes(player2Message.avatar, cc.SpriteFrame, function (err,sp) {
                if(err){
                    cc.error(err.message);
                }
                player222Avatar.spriteFrame = sp;
            });
            this.player2Name.string = player2Message.name;
            this.player2gameID.string = roomUserInfo.userID;
            playerData.gamePlayer.player2ID = roomUserInfo.userID;
            playerData.gamePlayer.playerNumber++;
            this.player2douziNum.string = player2Message.huanledouNumber;
            playerData.gamePlayer.player2douzhiNum = player2Message.huanledouNumber
            this.player2Owner.string = '';
            this.player2.active = true;
            this.p2 = true;
            
        }

        if(!this.p2 && !this.p3){
           var playerMessage = this.findPlayerMessage(roomUserInfo.userProfile);
           var player22Avatar = this.player2Avatar;
           cc.loader.loadRes(playerMessage.avatar, cc.SpriteFrame, function (err,sp) {
                if(err){
                    cc.error(err.message);
                }

                player22Avatar.spriteFrame = sp;
            });
            this.player2Name.string = playerMessage.name;
            this.player2gameID.string = roomUserInfo.userID;
            playerData.gamePlayer.player2ID = roomUserInfo.userID;
            playerData.gamePlayer.playerNumber++;
            this.player2douziNum.string = playerMessage.huanledouNumber;
            playerData.gamePlayer.player2douzhiNum = playerMessage.huanledouNumber
            this.player2Owner.string = '';
            this.player2.active = true;
            this.p2 = true;
        }
    },

    findPlayerMessage: function (playerName) {
        for (var i =0; i<playerData.player.length; i++){
            if(playerData.player[i].name == playerName) {
                var playerMessage = playerData.player[i];
                break;
            }
        }
        return playerMessage;
    },

    /**
     * 添加地主牌到 地主手上
     * 先计算牌的位置，移动，检查地主牌的位置，显示地主牌 ，地主牌的实例化在 initdizhupai2()中完成
     * @param {没加地主牌前的 牌数据} oldArr 
     * @param {没加地主牌前的 实例化牌} oldArrPrefab 
     */
    addCard: function (oldArr,oldArrPrefab) {
        for (var i =17;i < 20; i++) {
            playerData.gamePlayer.dizhupaiArr[i-17][3] = null;
            playerData.gamePlayer.dizhupaiArr[i- 17][3] = playerData.presentPlayer.gameID;//给三张地主牌加上 地主ID
            playerData.gamePlayer.player1pai[i] = playerData.gamePlayer.dizhupaiArr[i -17]; //将三张牌 的加入到玩家的牌中
        }
        newArr = playerData.gamePlayer.player1pai;          //将新的玩家的牌 赋值给 newArr, 方便操作
        this.pukepaiEar.paixu(newArr,2);                    //对牌进行排序
        this.pukepaiEar.setPlayerCard(newArr);              //将新的 牌数据传入 ，更新pukepaiEar 中的数据
        var str = this.getString(newArr);                   //对二维数组 string ，确保和 oldArr 一致性                 
        newArr = this.getArray(str);                        //string 转 二维数组
        var newPos = 960/2 - ((newArr.length -1)/2) *30;    //计算新牌的初始位置
        var h =0;
        //对oldArr 和 newArr 的第一项进行对比(唯一性)， 如果相同，则将牌移动到新位置
        //如果不同，则说明是地主牌，将地主牌添加到 此区域，并设置对应位置
        //k<=17 存在 oldArr 是最后一个但是newArr 却不是的情况， 需要对这个情况处理
        for (var i =0, k =0; i<20 && k<=17; i++) {          
           if( k!=17) {
               if(oldArr[k][0] == newArr[i][0] ) {
                   var action = cc.moveTo(0.15,cc.v2(newPos + i*30, 75));  //设置移动目标位置
                    oldArrPrefab[k].runAction(action);                      //执行移动
                    k++;  
               }else {
                    cc.find("Canvas/pukepaiEar").addChild(playerData.gamePlayer.addDizhupai[h]);
                    playerData.gamePlayer.addDizhupai[h].position = cc.v2(newPos + i*30,95);
                    var action = cc.moveTo(0.2,cc.v2(playerData.gamePlayer.addDizhupai[h].x,75));
                    playerData.gamePlayer.addDizhupai[h].runAction(action);
                    h++;
               }       
           }
           else if(k==17 && i<20) {           //当k== 17 说明，地主牌插入的位置在最后面
            cc.find("Canvas/pukepaiEar").addChild(playerData.gamePlayer.addDizhupai[i-17]);
            playerData.gamePlayer.addDizhupai[i-17].position = cc.v2(newPos + i*30,95);
            var action = cc.moveTo(0.2,cc.v2(playerData.gamePlayer.addDizhupai[i-17].x,75));
            playerData.gamePlayer.addDizhupai[i-17].runAction(action);
           }
        }
        var j =0;
        //这里更新 initpai ,不能在上面的for里面 更新， 必须先实例化后再添加
        //这里数据存在一个实时的更新问题。 oldArrPrefab 和 playerData.gamePlayer.initpai 数据是相同的，统一的
        //两个中的任何一个发生改变都会导致另一个改变
        for (var i =0, k =0; i<20 && k<=17; i++) {
            if(k!=17) {
                if(oldArr[k][0] == newArr[i][0]) {
                    k++;
                }else {
                    var temp = playerData.gamePlayer.initPai[16+j];
                    for(var n=16+j; n>=i; n--) {
                    playerData.gamePlayer.initPai[n+1] = playerData.gamePlayer.initPai[n];
                    }
                    playerData.gamePlayer.initPai[i] = playerData.gamePlayer.addDizhupai[j];
                    j++;
                }
            } 
            else if (k==17 && i<20) {
                playerData.gamePlayer.initPai[i] = playerData.gamePlayer.addDizhupai[i-17];
            }
            playerData.gamePlayer.initPai[i].zIndex =i;
         }
     },

     delCard: function (delArr,arr) {
        var disPos = 600/2 - ((delArr.length -1)/2) *15;
         for(var i in delArr) {
             var index = arr.indexOf(delArr[i]);
             arr.splice(index,1);
             //playerData.gamePlayer.player1pai.splice(index,1);
            cc.find('Canvas/pukepaiEar').removeChild(playerData.gamePlayer.initPai[index],false);    //不会清除remove掉的数据
            //console.log(playerData.gamePlayer.initPai[index].position);
            cc.find('Canvas/displayerCard').addChild(playerData.gamePlayer.initPai[index]);         //重新加入
            //console.log(playerData.gamePlayer.initPai[index].position);
            playerData.gamePlayer.initPai[index].zIndex = i;          //层级设置
            //需要重设位置
            playerData.gamePlayer.initPai[index].position = cc.v2(playerData.gamePlayer.initPai[index].x - 180,playerData.gamePlayer.initPai[index].y - 215);
            var action = cc.spawn(cc.scaleTo(0.2,0.4),cc.moveTo(0.2,cc.v2(disPos + i*15,0)));
            playerData.gamePlayer.initPai[index].runAction(action);
            //cc.find('Canvas/displayerCard').addChild(playerData.gamePlayer.initPai[index]);
            
             playerData.gamePlayer.initPai.splice(index,1);
             
         }
         console.log(arr.length,playerData.gamePlayer.initPai.length);
         for(var i in arr) {
             console.log(arr[i]);
         }

         var newPos = 960/2 - ((arr.length -1)/2) *30;
         for(var i =0; i< playerData.gamePlayer.initPai.length; i++) {
             var action = cc.moveTo(0.2,cc.v2(newPos + i*30,75));
             playerData.gamePlayer.initPai[i].runAction(action);
            //playerData.gamePlayer.initPai[i].position = cc.v2(newPos + i*30,75);
         }
     },

     playerCardDisInst: function (weizhi, i) {
        cc.loader.loadRes('prefab/pukepai_'+weizhi, cc.Prefab, function(err, prefab) {
            if(err) {
                cc.error(err);
                return;
            }
            var card = cc.instantiate(prefab);
            displayCard[i] = card;
        })
     },

     player3CardDis: function (arr) {
        cc.find('Canvas/player3DisCard').removeAllChildren(true);
         var pre ;
         console.log('kkkk');
         for(var i in arr) {
            var index = arr[i][0];
            console.log(arr[i]);
            pre = cc.instantiate(displayCard[index]) ;
            cc.find('Canvas/player3DisCard').addChild(pre);
            pre.position = cc.v2(-50,0);
            var action = cc.spawn(cc.scaleTo(0.2,0.4),cc.moveTo(0.2,cc.v2(i*15,0)));
            pre.runAction(action);
         }   
     },

     player2CardDis: function (arr) {
         cc.find('Canvas/player2DisCard').removeAllChildren(true);
         var pre;
         for(var i in arr) {
             var index = arr[i][0];
             pre = cc.instantiate(displayCard[index]);
             cc.find('Canvas/player2DisCard').addChild(pre);
             pre.zIndex = arr.length - i;   
             pre.position = cc.v2(50,0);
             var action = cc.spawn(cc.scaleTo(0.2,0.4),cc.moveTo(0.2,cc.v2(-i*15,0)));
             pre.runAction(action);
         }
     }

     



});
